/*
 * Baseado no exemplo publicado em http://mootools.net/blog/2010/05/18/.
*/
Fx.Diff = new Class({

    Extends: Fx,

    count: 0,

    render: null,

    set: function(now) {
      now = now.round();
      var diff = now - this.count;
      if (diff) {
        this.render(diff, now);
        this.count += diff;
      }
      return this;
    }
  });

Fx.Typewriter = new Class({

    Extends: Fx.Diff,

    options: {
      useCurrentText: true,
           charSpeed: 100,
          transition: Fx.Transitions.Linear,
                link: 'ignore'
    },

    initialize: function(element, options) {
      this.element = document.id(element);
      this.parent(options);
      if (this.options.useCurrentText) {
        this.mkTextArray( this.element.get('text') );
        this.element.set('text', '');
      }
    },

    start: function(text) {
      if (text) this.mkTextArray(text);
      this.parent(0, this.textArray.length);
      return this;
    },

    render: function(diff, now) {
      var text = this.textArray.filter(
        function(item, index) { return index < now });
      this.element.set('text', text.join(''));
    },

    mkTextArray: function(text) {
      this.textArray = text.split('');
      this.options.duration = this.textArray.length * this.options.charSpeed;
    },

    toggle: function() {
      if (this.element.get('text').length == 0) {
        this.start();
      } else {
        this.element.set('text', '');
      }
    }

  });